package com.auxgroup.hr.front.commons.utils;

import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebServiceClient {

	static int socketTimeout = 30000;// 请求超时时间
	static int connectTimeout = 30000;// 传输超时时间
	private static final Logger logger = LoggerFactory
			.getLogger(WebServiceClient.class);

	public static void main(String[] args) throws Exception {
		String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <soapenv:Envelope  xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body>"+
					"<C_WFL_APPROVAL_RESP xmlns=\"http://xmlns.oracle.com/Enterprise/Tools/schemas/C_WFL_APPROVAL_RESP.V1\"><C_MSG_CODE>0</C_MSG_CODE><C_MSG_TEXT>成功</C_MSG_TEXT></C_WFL_APPROVAL_RESP></soapenv:Body> </soapenv:Envelope>";
		org.dom4j.Document doc = DocumentHelper.parseText(result);
		Element rootel = doc.getRootElement();
		System.out.println("root="+rootel.getName());
		System.out.println("root="+rootel.elementIterator().toString());
		Element body = (Element)rootel.elementIterator().next();
		Element resp = body.element("C_WFL_APPROVAL_RESP");
		System.out.println("root="+resp.elementTextTrim("C_MSG_TEXT"));
		
	}

	/**
	 * 使用SOAP1.1发送消息
	 * 
	 * @param postUrl
	 * @param soapXml
	 * @param soapAction
	 * @return
	 */
	public static String doPostSoap(String postUrl, String soapXml,
			String soapAction) {
		logger.info("WebService request soapaction:{}, soapXml:{}",soapAction,soapXml);
		String retStr = "";
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpPost httpPost = new HttpPost(postUrl);
		// 设置请求和传输超时时间
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(socketTimeout)
				.setConnectTimeout(connectTimeout).build();
		httpPost.setConfig(requestConfig);
		try {
			httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
			httpPost.setHeader("SOAPAction", soapAction);
			StringEntity data = new StringEntity(soapXml,
					Charset.forName("UTF-8"));
			httpPost.setEntity(data);
			CloseableHttpResponse response = closeableHttpClient
					.execute(httpPost);
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				// 打印响应内容
				retStr = EntityUtils.toString(httpEntity, "UTF-8");
				logger.info("WebService response :{}" , retStr);
			}
			// 释放资源
			closeableHttpClient.close();
		} catch (Exception e) {
			logger.error("WebService error", e);
		}
		return retStr;
	}
}